package com.moss.cloud.auth.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moss.cloud.auth.biz.mapper.SysRoleMapper;
import com.moss.cloud.auth.biz.service.ISysRoleService;
import com.moss.cloud.auth.biz.service.ISysUserRoleService;
import com.moss.cloud.common.base.model.po.SysRole;
import com.moss.cloud.common.base.model.po.SysUserRole;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 角色服务层实现
 * @author 瑾年
 * @data 2023年3月6日
 */
@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper,SysRole> implements ISysRoleService {
    @Resource
    ISysUserRoleService sysUserRoleService;
    /**
     * 用户的权限集合
     * @param userId
     * @return
     */
    @Override
    public List<SysRole> queryUserRolesByUserId(long userId) {
        List<SysUserRole> userRoles = sysUserRoleService.list(new QueryWrapper<SysUserRole>()
                .lambda()
                .eq(SysUserRole::getUserId, userId));
        if(CollectionUtils.isNotEmpty(userRoles)){
            return this.list(new QueryWrapper<SysRole>().lambda().in(SysRole::getId,userRoles.stream()
                    .map(userRole -> userRole.getRoleId()).collect(Collectors.toList())));
        }
        return Collections.emptyList();
    }
}
